package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.page.PageData;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.common.utils.ConvertUtils;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dao.StationDao;
import io.aircraft.modules.wms.dto.StationDTO;
import io.aircraft.modules.wms.entity.StationEntity;
import io.aircraft.modules.wms.service.StationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 站台信息
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-12-12
 */
@Service
public class StationServiceImpl extends CrudServiceImpl<StationDao, StationEntity, StationDTO> implements StationService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageData<StationDTO> page(Map<String, Object> params) {
        IPage<StationEntity> page = baseDao.selectPage(
                getPage(params, "station_id", true),
                getWrapper(params)
        );
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, StationDTO.class);
    }

    @Override
    public QueryWrapper<StationEntity> getWrapper(Map<String, Object> params) {
        String stationNo = (String) params.get("stationNo");
        String stationType = (String) params.get("stationType");
        String stationDescription = (String) params.get("stationDescription");

        QueryWrapper<StationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(stationType), "station_type", stationType);
        wrapper.like(StringUtils.isNotBlank(stationNo), "station_no", stationNo);
        wrapper.like(StringUtils.isNotBlank(stationDescription), "station_description", stationDescription);

        return wrapper;
    }

    @Override
    public List<StationDTO> selectStnList(Map<String, Object> params) {
        String stationType = (String) params.get("stationType");
        QueryWrapper<StationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(stationType), "station_type", stationType);
        wrapper.eq("status", 0);
        List<StationEntity> entityList = baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, StationDTO.class);
    }
}